<template>
  <!-- 用了一个行列布局 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 左边 -->
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>

    <!-- 右边 -->
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <!-- ---- -->
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <!-- ---- -->
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!root"
                command="edit"
              >编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!root"
                command="del"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'

export default {
  props: {
    //   传过来渲染的信息
    nodeData: {
      type: Object,
      required: true
    },
    // 是展示 删除 编辑
    root: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(type) {
      // 添加
      if (type === 'add') {
        // nodeData穿过给父级部门的
        this.$emit('showAddDept', this.nodeData)
      }
      //   编辑
      if (type === 'edit') {
        // nodeData为了编辑自己的部门信息,给自己用
        this.$emit('editDept', this.nodeData)
      }
      //   删除
      if (type === 'del') {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        })
          .then(async() => {
            const res = await reqDelDepartments(this.nodeData.id)
            if (res.code !== 10000) return
            this.$message.success('删除完成')
            this.$emit('delList')
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      }
    }
  }
}
</script>

<style>
</style>
